import time
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.export_records_locator import ExportRecordsLocator
from Case.python_ui.PageLocate.user_orders_locator import OrdersLocator
from selenium.webdriver.common.keys import Keys
from Case.python_ui.PageLocate.admin_my_resource_locator import AdminMyResourceLocator



class  ExportRecordsPage(Basepage):

    def enter_ExportRecords_page(self):
        doc = '进入到ExportRecords页面'
        for i in range(3):
            try:
                eles = self.get_elements(ExportRecordsLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ExportRecordsLocator.export_records_tab, doc)
                self.click_element(ExportRecordsLocator.export_records_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)

    def enter_user_ExportRecords(self):
        doc = 'user端进入到ExportRecords页面'
        for i in range(3):
            try:
                eles = self.get_elements(ExportRecordsLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ExportRecordsLocator.user_export_records_tab, doc)
                self.click_element(ExportRecordsLocator.user_export_records_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)

    def export_records_resetstatus(self):
        doc = 'ExportRecords页面清空status筛选条件'
        ##先重置status的状态清空
        time.sleep(2)
        try:
            self.wait_elevisble(ExportRecordsLocator.status_filter, doc)
            self.move_mouse_to_click_element(ExportRecordsLocator.status_filter, doc)
            self.wait_elevisble(ExportRecordsLocator.reset_btn, doc)
            self.click_element(ExportRecordsLocator.reset_btn, doc)
        except:
            pass
        self.wait_elevisble(ExportRecordsLocator.ok_btn, doc)
        self.click_element(ExportRecordsLocator.ok_btn, doc)

    def export_records_download(self):
        doc = 'ExportRecords页面检查download功能'
        self.wait_elevisble(ExportRecordsLocator.status_filter, doc)
        self.move_mouse_to_click_element(ExportRecordsLocator.status_filter, doc)
        self.wait_elevisble(ExportRecordsLocator.select_success, doc)
        self.click_element(ExportRecordsLocator.select_success, doc)
        self.wait_elevisble(ExportRecordsLocator.ok_btn, doc)
        self.click_element(ExportRecordsLocator.ok_btn, doc)
        time.sleep(2)
        self.wait_elevisble(ExportRecordsLocator.download_btn, doc)
        self.click_element(ExportRecordsLocator.download_btn, doc)
    def export_records_expired(self):
        doc = 'Exporting Record状态过期校验'
        self.wait_elevisble(ExportRecordsLocator.status_filter, doc)
        self.move_mouse_to_click_element(ExportRecordsLocator.status_filter, doc)
        self.wait_elevisble(ExportRecordsLocator.select_expired, doc)
        self.click_element(ExportRecordsLocator.select_expired, doc)
        self.wait_elevisble(ExportRecordsLocator.ok_btn, doc)
        self.click_element(ExportRecordsLocator.ok_btn, doc)
        time.sleep(2)
        self.wait_elevisble(ExportRecordsLocator.file_status_ele, doc)
        file_status = self.get_element(ExportRecordsLocator.file_status_ele, doc).text
        assert file_status == 'File is expired'

    def export_records_filter(self):
        doc = 'Exporting Record界面status筛选功能校验'
        self.wait_elevisble(ExportRecordsLocator.status_filter, doc)
        self.move_mouse_to_click_element(ExportRecordsLocator.status_filter, doc)
        self.wait_elevisble(ExportRecordsLocator.select_expired, doc)
        self.click_element(ExportRecordsLocator.select_expired, doc)
        self.click_element(ExportRecordsLocator.select_success, doc)
        self.click_element(ExportRecordsLocator.select_failed, doc)
        self.wait_elevisble(ExportRecordsLocator.ok_btn, doc)
        self.click_element(ExportRecordsLocator.ok_btn, doc)
        time.sleep(2)

    def enter_admin_orders_page(self):
        doc = 'admin进入到orders页面'
        for i in range(3):
            try:
                eles = self.get_elements(ExportRecordsLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ExportRecordsLocator.order_management_tab, doc)
                self.click_element(ExportRecordsLocator.order_management_tab, doc)
                self.wait_elevisble(ExportRecordsLocator.admin_orders, doc)
                self.click_element(ExportRecordsLocator.admin_orders, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)

    def enter_admin_myresource(self):
        doc = 'admin进入到myresource页面'
        for i in range(3):
            try:
                eles = self.get_elements(ExportRecordsLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ExportRecordsLocator.order_management_tab, doc)
                self.click_element(ExportRecordsLocator.order_management_tab, doc)
                self.wait_elevisble(ExportRecordsLocator.admin_myresource, doc)
                self.click_element(ExportRecordsLocator.admin_myresource, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
    def enter_admin_income(self):
        doc = '进入到admin端income/Expense页面'
        for i in range(3):
            try:
                eles = self.get_elements(ExportRecordsLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(ExportRecordsLocator.bill_tab, doc)
                self.click_element(ExportRecordsLocator.bill_tab, doc)
                self.wait_elevisble(ExportRecordsLocator.admin_income, doc)
                self.click_element(ExportRecordsLocator.admin_income, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
    def enter_user_income(self):
        doc = '进入到user端income/Expense页面'
        for i in range(3):
            try:
                self.wait_elevisble(ExportRecordsLocator.user_income, doc)
                self.click_element(ExportRecordsLocator.user_income, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
    def enter_user_orders(self):
        doc = '进入到user端orders页面'
        for i in range(3):
            try:
                self.wait_elevisble(ExportRecordsLocator.user_orders_tab, doc)
                self.click_element(ExportRecordsLocator.user_orders_tab, doc)
                break
            except:
                self.refresh_page()
                continue


    def enter_start_end_time(self, startdate, enddate, doc):
        self.wait_elevisble(ExportRecordsLocator.startdate_ele, doc)
        self.send_keys(ExportRecordsLocator.startdate_ele, startdate, doc)
        self.wait_elevisble(ExportRecordsLocator.enddate_ele, doc)
        self.send_keys(ExportRecordsLocator.enddate_ele, enddate, doc)

    def admin_orders_export_time(self):
       doc = 'Order 界面选择时间范围大于6个月'
       time.sleep(2)
       self.wait_elevisble(ExportRecordsLocator.startdate_ele, doc)
       self.send_keys(ExportRecordsLocator.startdate_ele, '01-01-2024', doc)
       self.wait_elevisble(ExportRecordsLocator.enddate_ele, doc)
       self.send_keys(ExportRecordsLocator.enddate_ele, '01-11-2024', doc)
       time.sleep(3)
       self.wait_elevisble(ExportRecordsLocator.orders_download, doc)
       self.click_element(ExportRecordsLocator.orders_download, doc)
       self.wait_elevisble(ExportRecordsLocator.error_message_ele, doc)
       retrun_mess = self.get_element(ExportRecordsLocator.error_message_ele, doc).text
       print(retrun_mess)
    ####TL672073 | Order界面下载时时间范围的限制
    def user_orders_export_time(self):
       doc = 'Order界面下载时时间范围的限制'
       time.sleep(2)
       ##进入order界面
       self.wait_elevisble(OrdersLocator.orders_tab, doc)
       self.click_element(OrdersLocator.orders_tab, doc)
       time.sleep(2)
       self.wait_elevisble(ExportRecordsLocator.startdate_ele,doc)
       self.move_mouse_to_element(ExportRecordsLocator.startdate_ele)
       time.sleep(2)
       ##直接下载提示需要输入时间范围
       try:
            self.wait_elevisble(ExportRecordsLocator.clear_time,doc,time=10)
            self.move_mouse_to_click_element(ExportRecordsLocator.clear_time, doc)
            time.sleep(5)
            self.move_mouse_to_click_element(ExportRecordsLocator.order_search, doc)
       except:
           print('>>>>>>>>>>>>>>>>>')
           pass
       time.sleep(2)
       self.wait_elevisble(ExportRecordsLocator.user_orders_download, doc)
       self.click_element(ExportRecordsLocator.user_orders_download, doc)
       self.wait_elevisble(ExportRecordsLocator.error_message_ele, doc)
       return_mess = self.get_element(ExportRecordsLocator.error_message_ele, doc).text
       print(return_mess)
       time.sleep(3)
       #输入时间范围大于6个月
       self.enter_start_end_time('01-01-2024','01-12-2024', doc)
       time.sleep(3)
       self.wait_eleinvisble(ExportRecordsLocator.loading_button, doc)
       self.wait_elevisble(ExportRecordsLocator.user_orders_download, doc)
       self.click_element(ExportRecordsLocator.user_orders_download, doc)
       self.wait_elevisble(ExportRecordsLocator.error_message_ele, doc)
       return_mess = self.get_element(ExportRecordsLocator.error_message_ele, doc).text
       print(return_mess)
       time.sleep(3)
       ##输入时间范围小于6个月可以下载
       self.move_mouse_to_element(ExportRecordsLocator.startdate_ele)
       #self.click_element(ExportRecordsLocator.clear_time, doc)
       try:
           self.wait_elevisble(ExportRecordsLocator.clear_time, doc, time=10)
           self.move_mouse_to_click_element(ExportRecordsLocator.clear_time, doc)
           time.sleep(5)
           self.move_mouse_to_click_element(ExportRecordsLocator.order_search, doc)
       except:
           pass
       self.enter_start_end_time('01-01-2024','01-02-2024', doc)
       time.sleep(5)
       self.wait_eleinvisble(ExportRecordsLocator.loading_button, doc)
       self.click_element(ExportRecordsLocator.order_search, doc)
       self.wait_elevisble(ExportRecordsLocator.user_orders_download, doc)
       self.click_element(ExportRecordsLocator.user_orders_download, doc)
       time.sleep(2)
       self.wait_elevisble(ExportRecordsLocator.export_records_title, doc)
       export_title = self.get_element(ExportRecordsLocator.export_records_title, doc).text
       print(export_title)
       assert export_title == 'Export Records'

    def user_orders_null_download(self):
        doc = 'Order界面内容为空，下载按钮置灰无法下载'
        time.sleep(2)
        ##进入order界面
        self.wait_elevisble(OrdersLocator.orders_tab, doc)
        self.click_element(OrdersLocator.orders_tab, doc)
        time.sleep(2)
        self.wait_elevisble(ExportRecordsLocator.orderid_search, doc)
        self.send_keys(ExportRecordsLocator.orderid_search, 111111, doc)
        time.sleep(2)
        #self.wait_elevisble(ExportRecordsLocator.order_search, doc)
        #self.click_element(ExportRecordsLocator.order_search, doc)
        self.send_keys(ExportRecordsLocator.orderid_search, Keys.RETURN, doc)
        time.sleep(3)
        self.wait_eleinvisble(ExportRecordsLocator.loading_button, doc)
        time.sleep(3)
        self.get_element(ExportRecordsLocator.download_pray, doc)
    def user_income_null_download(self):
        doc = 'income/expense界面内容为空，下载按钮置灰无法下载'
        self.wait_elevisble(ExportRecordsLocator.startdate1, doc)
        self.send_keys(ExportRecordsLocator.startdate1, '10-01-2018', doc)
        self.wait_elevisble(ExportRecordsLocator.enddate1,  doc)
        self.send_keys(ExportRecordsLocator.enddate1, '10-02-2018', doc)
        time.sleep(3)
        self.wait_eleinvisble(ExportRecordsLocator.loading_button, doc)
        time.sleep(3)
        self.get_element(ExportRecordsLocator.download_pray, doc)
    def admin_income_export_time(self):
        doc = 'Income/expense界面选择时间范围大于6个月'
        time.sleep(2)
        self.wait_elevisble(ExportRecordsLocator.startdate_ele, doc)
        self.send_keys(ExportRecordsLocator.startdate_ele, '01-01-2024', doc)
        self.wait_elevisble(ExportRecordsLocator.enddate_ele, doc)
        self.send_keys(ExportRecordsLocator.enddate_ele, '01-11-2024', doc)
        time.sleep(3)
        self.wait_elevisble(ExportRecordsLocator.income_downdload, doc)
        self.click_element(ExportRecordsLocator.income_downdload, doc)
        self.wait_elevisble(ExportRecordsLocator.error_message_ele, doc)
        return_mess = self.get_element(ExportRecordsLocator.error_message_ele, doc).text
        print(return_mess)

    def admin_resource_export_time(self):
        doc = 'admin端my resource时间范围大于6个月下载，不校验'
        time.sleep(2)
        self.wait_elevisble(ExportRecordsLocator.startdate1, doc)
        self.send_keys(ExportRecordsLocator.startdate1, '01-01-2024', doc)
        self.wait_elevisble(ExportRecordsLocator.enddate1, doc)
        self.send_keys(ExportRecordsLocator.enddate1, '01-11-2024', doc)
        self.wait_elevisble(ExportRecordsLocator.myresource_search, doc)
        self.click_element(ExportRecordsLocator.myresource_search, doc)
        time.sleep(3)
        self.wait_elevisble(ExportRecordsLocator.myresource_download, doc)
        self.click_element(ExportRecordsLocator.myresource_download, doc)
        time.sleep(2)
        self.wait_elevisble(ExportRecordsLocator.export_records_title, doc)
        export_title = self.get_element(ExportRecordsLocator.export_records_title, doc).text
        assert export_title == 'Export Records'







